import { Component, Input, Output, OnInit } from '@angular/core';

import { FileUploader } from 'ng2-file-upload';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'app-file-uploader-single',
  templateUrl: './file.uploader.single.component.html',
  styleUrls: ['./file.uploader.single.component.css']
})
export class FileUploaderSingleComponent implements OnInit {

  uploader: FileUploader;
  imageUrl;
  @Input() accept = 'image/png, image/jpeg, image/jpg';

  file = {
    name: '',
    size: '',
    filePath: '',
  };

  constructor(private sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.uploader = new FileUploader({
      url: '/file/upload/image',
      method: 'POST',
      allowedFileType: ['image'],
      autoUpload: true,
      maxFileSize: 1024000, // 单位是字节
    });
  }

  onFileChanged(event) {
    let files = event.currentTarget.files;
    this.imageUrl = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(files[0]));
  }

}
